/*********************************/
/** MOS 6502.ts author hansong  **/
/*********************************/
//内存构造器
var Memory = /** @class */ (function () {
    //初始化内存
    function Memory(maxMemory) {
        this.data = new ArrayBuffer(maxMemory);
    }
    //读内存
    Memory.prototype.read = function (address) {
        return this.data[address];
    };
    //写内存
    Memory.prototype.write = function (address, value) {
        var temp = new Uint8Array(1);
        temp[0] = value;
        this.data[address] = temp[0];
    };
    return Memory;
}());
var memory = new Memory(64 * 1024);
memory.write(222, 23);
console.log(memory.read(222));
/********** Debug tools **********/
// Debug mode, disabled by default
var debug_mode = false;
// Trace mode (step-by-step execution), disabled by default
var trace_mode = false;
// R8[0] = AC
// R8[1] = Y
// R8[2] = X
// R8[3] = ST
var R8 = new Uint8Array(4);
// R16[1] = SP
// R16[2] = PC
var R16 = new Uint16Array(2);
console.log(b(2, 0, 1));
// x(): write a number in hexadecimal
// @param n: the number
// @param i (optional): the length of the hexadecimal value, with leading zeros (max: 8)
function x(n, i) { return ((i ? 1e7 : "") + n.toString(16).toUpperCase()).slice(-i); }
/********** Binary tools **********/
// lshift()
// @params a, b
// @return a << b
function lshift(number, shift) { return number * Math.pow(2, shift); }
// rshift()
// @params a, b
// @return a >> b
function rshift(number, shift) { return Math.floor(number / Math.pow(2, shift)); }
// b()
// @params number, start, end
// @return a number representing the bits of number read from start to end
// end param is optional. By default, one bit is read.
function b(number, start, end) { return rshift(number, start) & (Math.pow(2, (end || start) - start + 1) - 1); }
// ror()
// @params number, length, bits
// @return the number (which is "length" bits long) after a right rotation (make the "bits" first bits of the number, last) 
function ror(number, length, bits) { return lshift((number & Math.pow(2, bits) - 1), length - bits) + rshift(number, bits); }
